<?php
/**
 * (ajax)
 */
namespace App\Admin\Ajax\ProductsStockup;


use App\Jobs\ProductPickup\PickupOrderCreate;
use App\Jobs\ProductStockup\StockupOrderCreate;
use App\Models\ProductStockup\Stockup;
use App\Models\ProductStockup\StockupApplyLog;
use Encore\Admin\Facades\Admin;
use Illuminate\Support\Facades\DB;

class StockupAjax{

    /**
     * 创建申请
     * @param $param
     * @return \Illuminate\Http\JsonResponse
     */
    public function stockupApplyBatchCreate($param){
        echo "<pre>";
        print_r(111);
        echo "</pre>";
        exit;
        if (empty($param)) return response()->json(['status' => false, 'message' => '数据异常!']);
        try{
            DB::beginTransaction();

            $stockupApplyData = json_decode(json_encode($param));
            $count = 0;
            $userId = Admin::user()->id;
            foreach ($stockupApplyData as $key => $val){
                foreach ($val as $k => $v){
                    $stockup = Stockup::query()->where('id', $v->stockup_id)->first();
                    if ($stockup->status >= Stockup::STATUS_APPROVE) {
                        continue; //过滤，未审批和未确认
                    }
                    $stockup->apply_num = $v->num ?? 0;
                    $stockup->apply_remark = $v->remark ?? '';
                    $stockup->apply_admin_id = $userId;
                    $stockup->status = Stockup::STATUS_APPLY; //申请中
                    if (!$stockup->save()) throw new \Exception('申请失败!');

                    //日志
                    StockupApplyLog::_addStockupLog($stockup);
                }
                $count++;
            }

            DB::commit();
            return response()->json(['status' =>true, 'message' => "{$count}条备货申请，申请成功！"]);

        }catch (\Exception $e){
            DB::rollBack();
            report($e);
            return response()->json(['status' => false, 'message' => '数据异常!']);
        }
    }

    /**
     * 取消申请
     * @param $param
     * @return \Illuminate\Http\JsonResponse
     */
    public function StockupApplyBatchCancel($param){

        if (empty($param)) return response()->json(['status' => false, 'message' => '数据异常!']);
        try{
            DB::beginTransaction();

            $stockupApplyData = json_decode(json_encode($param));
            $count = 0;
            $userId = Admin::user()->id;

            foreach ($stockupApplyData as $key => $val){
                foreach ($val as $k => $v){
                    $stockup = Stockup::query()->find($v->stockup_id);
                    if ($stockup->status >= Stockup::STATUS_APPROVE) continue; //过滤，未审批和未确认

                    $stockup->apply_num = $v->num ?? 0;
                    $stockup->apply_remark = $v->remark ?? '';
                    $stockup->apply_admin_id = $userId;
                    $stockup->status = Stockup::STATUS_CANCEL; //撤回申请
                    if (!$stockup->save()) throw new \Exception('撤回申请失败!');

                    //日志
                    StockupApplyLog::_addStockupLog($stockup);
                }
                $count++;
            }

            DB::commit();
            return response()->json(['status' =>true, 'message' => "{$count}条备货申请，撤回成功！"]);

        }catch (\Exception $e){
            DB::rollBack();
            report($e);
            return response()->json(['status' => false, 'message' => $e->getMessage()]);
        }
    }

    /**
     * 审核拒绝
     * @param $param
     * @return \Illuminate\Http\JsonResponse
     */
    public function stockupApplyBatchReject($param){

        if (empty($param)) return response()->json(['status' => false, 'message' => '数据异常!']);
        try{
            DB::beginTransaction();

            $stockupApplyData = json_decode(json_encode($param));
            $count = 0;
            $userId = Admin::user()->id;
            foreach ($stockupApplyData as $key => $val){
                foreach ($val as $k => $v){
                    $stockup = Stockup::query()->find($v->stockup_id);
                    if ($stockup->status >= Stockup::STATUS_APPROVE) continue; //过滤，未审批和未确认

                    $stockup->apply_num = $v->num ?? 0;
                    $stockup->apply_remark = $v->remark ?? '';
                    $stockup->apply_admin_id = $userId;
                    $stockup->status = Stockup::STATUS_REJECT; //审核驳回
                    if (!$stockup->save()) throw new \Exception('驳回失败!');

                    //日志
                    StockupApplyLog::_addStockupLog($stockup);
                }
                $count++;
            }

            DB::commit();
            return response()->json(['status' =>true, 'message' => "{$count}条备货申请，驳回成功！"]);

        }catch (\Exception $e){
            DB::rollBack();
            report($e);
            return response()->json(['status' => false, 'message' => $e->getMessage()]);
        }
    }

    /**
     * 审核通过
     * @param $param
     * @return \Illuminate\Http\JsonResponse
     */
    public function StockupApplyBatchApprove($param){

        if (empty($param)) return response()->json(['status' => false, 'message' => '数据异常!']);
        try{
            DB::beginTransaction();

            $stockupApplyData = json_decode(json_encode($param));
            $count = 0;
            $userId = Admin::user()->id;
            foreach ($stockupApplyData as $key => $val){
                foreach ($val as $k => $v){
                    $stockup = Stockup::query()->find($v->stockup_id);
                    if ($stockup->status >= Stockup::STATUS_APPROVE) continue; //过滤，未审批和未确认

                    $stockup->approval_num = $v->num ?? 0;
                    $stockup->approval_remark = $v->remark ?? '';
                    $stockup->approval_admin_id = $userId;
                    $stockup->status = Stockup::STATUS_APPROVE; //审核通过
                    if (!$stockup->save()) throw new \Exception('审核失败!');

                    //日志
                    StockupApplyLog::_addStockupLog($stockup);
                }
                $count++;
            }

            DB::commit();
            return response()->json(['status' =>true, 'message' => "{$count}条备货申请，审核成功！"]);

        }catch (\Exception $e){
            DB::rollBack();
            report($e);
            return response()->json(['status' => false, 'message' => $e->getMessage()]);
        }
    }


    /**
     * 审核确认
     * @param $param
     * @return \Illuminate\Http\JsonResponse
     */
    public function StockupApplyBatchConfirm($param){

        if (empty($param)) return response()->json(['status' => false, 'message' => '数据异常!']);

        try{
            DB::beginTransaction();

            $count = 0;
            $userId = Admin::user()->id;
            $items = [];
            foreach ($param as $key => $val){
                foreach ($val as $k => $v){

                    $stockup = Stockup::query()->find($v['stockup_id']);
                    if ($stockup->status != Stockup::STATUS_APPROVE) continue; //过滤，未确认

                    $stockup->confirm_num = $v['num'] ?? 0;
                    $stockup->confirm_remark = $v['remark'] ?? '';
                    $stockup->confirm_admin_id = $userId;
                    $stockup->status = Stockup::STATUS_CONFIRM; //审核确认
                    if (!$stockup->save()) throw new \Exception('审核失败!');

                    //日志
                    StockupApplyLog::_addStockupLog($stockup);

                    $items[] = $stockup;
                }
                $count++;
            }

            ////确认之后生成备货单。。。
            //if(!empty($items)){
            //    StockupOrderCreate::dispatch($items);
            //}

            //生成提货单
            if(!empty($items)){
                PickupOrderCreate::dispatch($items);
            }

            DB::commit();
            return response()->json(['status' =>true, 'message' => "{$count}条备货申请，确认成功！"]);

        }catch (\Exception $e){
            DB::rollBack();
            report($e);
            return response()->json(['status' => false, 'message' => $e->getMessage()]);
        }
    }

}
